package cn.lzwei.bilimall.product.service.impl;

import cn.lzwei.bilimall.product.vo.AttrRelationVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;

import cn.lzwei.bilimall.product.dao.AttrAttrgroupRelationDao;
import cn.lzwei.bilimall.product.entity.AttrAttrgroupRelationEntity;
import cn.lzwei.bilimall.product.service.AttrAttrgroupRelationService;

import javax.annotation.Resource;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }
    /**
     * 批量删除属性与分组的关联
     */
    @Override
    public void deleteBatch(AttrRelationVo[] attrRelationVo) {
        //1.将VO数组转换为对应的Entry集合
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(attrRelationVo).map(item -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrGroupId(item.getAttrGroupId());
            entity.setAttrId(item.getAttrId());
            return entity;
        }).collect(Collectors.toList());
        //2.遍历集合进行删除
        attrAttrgroupRelationDao.deleteAttrGroupRelation(collect);
    }
    /**
     * 属性组批量关联属性
     */
    @Override
    public void addBatch(List<AttrRelationVo> relationVo) {
        //1.将 VO 转换为 Entity
        List<AttrAttrgroupRelationEntity> entities = relationVo.stream().map(item -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrId(item.getAttrId());
            entity.setAttrGroupId(item.getAttrGroupId());
            return entity;
        }).collect(Collectors.toList());
        //2.批量保存
        this.saveBatch(entities);
    }

}